package com.huiquan.icd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.icd.service.IcdDiseaseSymptomDescriptionService;

@Controller
public class IcdDiseaseSymptomDescriptionController {

	@Autowired
	private IcdDiseaseSymptomDescriptionService icdDiseaseSymptomDescriptionService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/disease_symptom_description_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("check_flag", "0");
			pageParam.put("countPerPage", "5");
			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String checkFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "check_flag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		User user = (User) req.getSession().getAttribute("user");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");

		return icdDiseaseSymptomDescriptionService.list(Integer.parseInt(start), keyword, countPerPageStr, user,
				checkFlag,exactSearchFlag);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_symptom_description_split.do")
	public ReturnData splitText(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String text = req.getParameter("text");
		User user = (User) req.getSession().getAttribute("user");
		try {
			return icdDiseaseSymptomDescriptionService.splitText(id, text, user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ReturnUtil.fail(ReturnCode.DYNAMIC_DESC_EXCEPTION);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_symptom_description_submit.do")
	public ReturnData submitPage(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("ids");
		User user = (User) req.getSession().getAttribute("user");
		return icdDiseaseSymptomDescriptionService.submitPage(id, user);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_symptom_description_change_array.do")
	public ReturnData changeArray(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String array = req.getParameter("array");
		User user = (User) req.getSession().getAttribute("user");
		return icdDiseaseSymptomDescriptionService.changeArray(id, array, user);
	}
}
